z<?php

require("./Conn.php");
require("./php_rgr0.php");

//create or replace PROCEDURE PRC002(C1 OUT SYS_REFCURSOR)
//        AS
//        NOMBRE varchar(30);
//        BEGIN
//        OPEN C1 FOR select fs.tablespace_name "Tablespace", (df.totalspace - fs.freespace) "Used MB",
//		fs.freespace "Free MB",round(100 * (fs.freespace / df.totalspace)) "Pct. Free" from(select tablespace_name,round(sum(bytes) / 1048576) TotalSpace from dba_data_files
//		group by tablespace_name) df,(select tablespace_name,round(sum(bytes) / 1048576) FreeSpace from dba_free_space group by tablespace_name) fs where 
//		df.tablespace_name = fs.tablespace_name;
//END PRC002 ;
//--/



if (!isset($_POST['req'])) {
    die("No request provided.");
} else {
    if (filter_input(INPUT_POST, 'req') == "Procedureando2") {
        
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        
        
        if (!$conn) {
            //print(json_encode("NO hay conexion"));
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        
       // print(json_encode("Encontro conexion"));
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin PRC002(:cursorAux); end;");
        oci_bind_by_name($stid, ":cursorAux", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);

        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0; 
        //$regresion = new RegresionLineal();
        while ($row = oci_fetch_array($curs, OCI_ASSOC)) {
            
            array_push($json['Elements'],$row );
            //$regresion->agrega($row['TABLESPACE_NAME'], $row['TABLE_NAME'], 100, $row['MB']);
            //array_push($json['Elements'], $regresion->getPorcUsado($row['TABLESPACE_NAME']));
            $i++;
        }

        if ($i > 0) {
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json['Elements']));

        //oci_free_statement($stid);
        //oci_free_statement($curs);
        //oci_close($conn);
    }
}
